﻿// -------------------------------------------------------------------------
//    @FileName         :    NFMallModule.cpp
//    @Author           :    Gao.Yi
//    @Date             :    2025-09-09
//    @Email			:    445267987@qq.com
//    @Module           :    NFMallModule
//    @Desc             :
//
// -------------------------------------------------------------------------

#include "NFMallModule.h"
#include "NFPacketSendUtil.h"
#include "NFPlayer.h"
#include "NFSession.h"
#include "proto_cs_cmd.nanopb.h"
#include "DescStore/ModuleControlDesc.h"

NFMallModule::NFMallModule(NFIPluginManager* p) : NFLogicDynamicModule(p)
{
}

NFMallModule::~NFMallModule()
{
}

int NFMallModule::Awake()
{
    RegisterClientMessage(NF_ST_LOGIC_SERVER, E_CS_MALL_LIST_REQ);
    return 0;
}

int NFMallModule::OnHandleClientMessage(NFDataPackage& packet, NFSession* pSession, const proto_ff::Proto_CSReq& req)
{
    CHECK_NULL(0, pSession);
    if (!m_pObjPluginManager->IsInited(NF_ST_LOGIC_SERVER))
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Logic Server not inited, drop client msg:{}", packet.ToString());
        return -1;
    }

    if (m_pObjPluginManager->IsServerStopping())
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Logic Server is Stopping, drop client msg:{}", packet.ToString());
        return ERR_CODE_SERVER_IS_STOPPING;
    }

    int iRetCode = 0;
    switch (packet.nMsgId)
    {
        case E_CS_MALL_LIST_REQ:
        {
            iRetCode = OnHandleMallListReq(packet, pSession, req);
            break;
        }
        default: NFLogError(NF_LOG_DEFAULT, 0, "not handle msg:{}", packet.nMsgId);
    }

    return iRetCode;
}

int NFMallModule::OnHandleMallListReq(NFDataPackage& packet, NFSession* pSession, const proto_ff::Proto_CSReq& req)
{
    CHECK_NULL(0, pSession);

    NFPlayer* pPlayer = pSession->GetPlayer();
    CHECK_NULL(0, pPlayer);

    auto pConfig = FindModule<NFIConfigModule>()->GetAppConfig(NF_ST_LOGIC_SERVER);
    CHECK_NULL(0, pConfig);

    const proto_ff::Proto_CSMallListReq& oCSMallListReq = req.mall_list_req();
    int iRegionID = NFServerIDUtil::GetRegionID(pConfig->GetBusId());
    auto pModuleControleDesc = ModuleControlDesc::Instance()->GetDescByWorldIdIdSubId(iRegionID, proto_ff::EN_MODULE_MALL, oCSMallListReq.type());
    if (pModuleControleDesc)
    {
        LOG_INFO(pPlayer->GetRoleID(), "mall type:{} is off!", oCSMallListReq.type());
        return proto_ff::ERR_CODE_MODULE_IS_OFF;
    }

    proto_ff::Proto_CSRsp oCSRsp;
    proto_ff::Proto_CSMallListRsp* pCSMallListRsp = oCSRsp.mutable_mall_list_rsp();
    CHECK_NULL(pPlayer->GetRoleID(), pCSMallListRsp);
    pCSMallListRsp->set_type(oCSMallListReq.type());

    return NFPacketSendUtil::SendToClient(pPlayer, E_CS_MALL_LIST_RSP, &oCSRsp);
}
